/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucm.model;

import cp3120_p.DAO;
import javax.swing.JOptionPane;
import java.sql.*;


public class UCRegister_Model {

    private DAO DbConn;
    private String UserName;
    private String PassWord;

    public UCRegister_Model(String UserName, String PassWord) {
        this.UserName = UserName;
        this.PassWord = PassWord;
        DbConn = new DAO();
    }

    public boolean runRegisger() {
        Connection con = DbConn.getConnection();
        if (con == null) {
            return false;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res = stmt.executeQuery("SELECT * FROM userinfo WHERE " +
                    "USERNAME = '" + UserName + "'");
            int a = res.getRow();
            if (res.getRow() == 0) {
                stmt.executeUpdate("INSERT INTO userinfo (USERNAME,PASSWORD) values('" + UserName + "', '" + PassWord + "')");
                JOptionPane.showMessageDialog(null, "Register Successful!");
                return true;
            }
            return false;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "This name already Used Choose another");
            return false;
        }
    }
}
